# encoding: utf-8
# @File  : test08_ht_login_fail.py
# @Author: wu shaofan
# @Date  :  2024/04/29
# @Desc :
import json
import allure
import pytest
import config
import conftest
from PO.page.page_ht_login import PageHtLogin
from Utils.get_data import CreateData
from Utils.get_driver import GetDriver
from Utils.record_log import Log


@allure.feature('4、后台登录')
class TestLogin:
    def setup_class(self):
        # TODO 获取driver
        self.driver = GetDriver().get_driver(config.ht_login_url)
        # 异常截图
        conftest.driver = self.driver
        # 获取登录对象
        self.login = PageHtLogin(self.driver)
        # 获取日志对象
        self.log = Log("后台登录失败用例").get_logger()

    def teardown_class(self):
        # 关闭driver
        GetDriver().close_driver()
        Log("后台登录失败用例").write_empty_line()

    # 参数化数据
    json_name = "ht_login"
    data = ('username', 'pwd', 'code', 'expect')
    obj = CreateData(json_name, data)
    data = obj.build_data()
    # 用例标题
    ids_data = obj.build_desc()

    @allure.story('2、登录失败')
    @pytest.mark.parametrize("username,pwd,code,expect", data, ids=ids_data)
    def test_login02_fail(self, username, pwd, code, expect):
        with allure.step('步骤一：输入用户名'):
            self.login.page_input_username(username)

        with allure.step('步骤二：输入密码'):
            self.login.page_input_password(pwd)

        with allure.step('步骤三：输入验证码'):
            self.login.page_input_verify_code(code)

        with allure.step('步骤四：点击登录按钮'):
            self.login.page_click_login_btn()

        with allure.step('步骤五：获取错误结果'):
            result = self.login.page_get_error_msg()
            self.login.base_get_allure_image()

        with allure.step('步骤六：断言'):
            assert result == expect

        data = {
            "登录失败记录": {
                "用户名": username,
                "密码": pwd,
                "验证码": code},
            "断言": {
                "预期结果": expect,
                "实际结果": result}
        }
        self.log.info(f"后台登录失败数据：{data}")
        data = json.dumps(data, ensure_ascii=False, indent=4)
        allure.attach(data, "后台登录失败数据", allure.attachment_type.JSON)


if __name__ == '__main__':
    pytest.main(['-s', 'test08_ht_login_fail.py'])
